select * from ( select * from ( select l.demographicNo, CONCAT( "" , d.demographic_no, "" ) As "Dem", CONCAT( "eChart" ) AS "eChart" , l.dateObserved, TIMESTAMPDIFF (YEAR, concat(year_of_birth,'-',month_of_birth,'-',date_of_birth), CURDATE()) as age, d.last_name, d.first_name, l.type, l.dataField,l.comments from measurements l, demographic d where d.patient_status = "AC" and l.demographicNo = d.demographic_no and ( l.type like '{lab}') order by demographic_no, dateObserved desc ) as t1 group by demographicNo order by dateObserved desc ) as t2 where (dataField {ac})